home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Movie / Sources / MoviePar.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  14.1 KB  |  485 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                MoviePar.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef MOVIEPAR_H
  11. #include "MoviePar.h"
  12. #endif
  13.  
  14. #ifndef MOVIEFRA_H
  15. #include "MovieFra.h"
  16. #endif
  17.  
  18. #ifndef MOVIESEL_H
  19. #include "MovieSel.h"
  20. #endif
  21.  
  22. #ifndef CMOVIE_H
  23. #include "CMovie.h"
  24. #endif
  25.  
  26. // ----- Framework Includes -----
  27.  
  28. #ifndef FWUTIL_H
  29. #include "FWUtil.h"
  30. #endif
  31.  
  32. #ifndef FWITERS_H
  33. #include "FWIters.h"
  34. #endif
  35.  
  36. // ----- OS Layer Includes -----
  37.  
  38. #ifndef FWGRAPHX_H
  39. #include "FWGraphx.h"
  40. #endif
  41.  
  42. #ifndef FWMENU_H
  43. #include "FWMenu.h"
  44. #endif
  45.  
  46. #ifndef FWALERT_H
  47. #include "FWAlert.h"
  48. #endif
  49.  
  50. #ifndef FWEVENT_H
  51. #include "FWEvent.h"
  52. #endif
  53.  
  54. #ifndef FWRESOUR_H
  55. #include "FWResour.h"
  56. #endif
  57.  
  58. #ifndef FWSTRING_H
  59. #include "FWString.h"
  60. #endif
  61.  
  62. #ifndef FWWINDOW_H
  63. #include "FWWindow.h"
  64. #endif
  65.  
  66. #ifndef FWCFMRES_H
  67. #include "FWCFMRes.h"
  68. #endif
  69.  
  70. #ifndef FWFILESP_H
  71. #include "FWFileSp.h"
  72. #endif
  73.  
  74. #ifndef FWODGEOM_H
  75. #include "FWODGeom.h"
  76. #endif
  77.  
  78. #ifndef FWSUSINK_H
  79. #include "FWSUSink.h"
  80. #endif
  81.  
  82. #ifndef FWBARRAY_H
  83. #include "FWBArray.h"
  84. #endif
  85.  
  86. #ifndef FWMEMMGR_H
  87. #include "FWMemMgr.h"
  88. #endif
  89.  
  90. // ----- OpenDoc Includes -----
  91.  
  92. #ifndef SOM_ODShape_xh
  93. #include <Shape.xh>
  94. #endif
  95.  
  96. #ifndef SOM_ODSession_xh
  97. #include <ODSessn.xh>
  98. #endif
  99.  
  100. #ifndef SOM_ODWindowState_xh
  101. #include <WinStat.xh>
  102. #endif
  103.  
  104. #ifndef SOM_Module_OpenDoc_StdProps_defined
  105. #include <StdProps.xh>
  106. #endif
  107.  
  108. // ----- Platform Includes -----
  109.  
  110. #if defined(FW_BUILD_MAC) && !defined(__MOVIES__)
  111. #include <Movies.h>
  112. #endif
  113.  
  114. //========================================================================================
  115. //    Defines
  116. //========================================================================================
  117.  
  118. #define kContentAnnotationProp        "Apple:ODFMovie:Property:ContentAnnotation"
  119. #define kContentAnnotationValue        "Apple:ODFMovie:Value:ContentAnnotation"
  120.  
  121. //========================================================================================
  122. //    Runtime informations
  123. //========================================================================================
  124.  
  125. #ifdef FW_BUILD_MAC
  126. #pragma segment odfmovie
  127. #endif
  128.  
  129. //========================================================================================
  130. // Constants
  131. //========================================================================================
  132.  
  133. const short kChooseMovieItemID = 1;
  134. const short kStartMovieItemID = 3;
  135. const short kStopMovieItemID = 4;
  136. const short kSetMovieLoopingItemID = 6;
  137. const short kShowMovieControllerItemID = 7;
  138.  
  139. const short kDefaultMoviePicture = 2128;
  140.  
  141. const short kMovieIdleFreq = 1;
  142.  
  143. const ODValueType CMoviePart::kPartKind = kODFMovieKind;
  144. const ODValueType CMoviePart::kPartUserName = kODFMovieEditorUserString;
  145.  
  146. //========================================================================================
  147. //    CLASS CMoviePart
  148. //========================================================================================
  149.  
  150. //----------------------------------------------------------------------------------------
  151. //     CMoviePart::CMoviePart
  152. //----------------------------------------------------------------------------------------
  153.  
  154. CMoviePart::CMoviePart(ODPart* odPart):
  155.     FW_CPart(odPart, CMoviePart::kPartKind, CMoviePart::kPartUserName, FW_gInstance, kPartIconID),
  156.     fDefaultPicture(),
  157.     fLooping(FALSE),
  158.     fShowController(TRUE),
  159.     fInternalizedMovie(NULL)
  160. {
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. //    CMoviePart::Initialize
  165. //----------------------------------------------------------------------------------------
  166.  
  167. void CMoviePart::Initialize(Environment* ev)
  168. {
  169.     FW_CPart::Initialize(ev);
  170.     
  171.     CMovie::InitializeQuickTime();    // Throw some error here
  172.     
  173.     fPresentation = RegisterPresentation(ev, "Apple:Presentation:ODFMovie", TRUE, new CMovieSelection(ev, this));
  174.     
  175.     FW_CSharedLibraryResourceFile resFile;    // Open the resource file of this shared library
  176.  
  177.     // ----- Arrange Menu -----
  178.     FW_CPullDownMenu* pullDownMenu = new FW_CPullDownMenu(ev, resFile, kMenuStrings, kMovieStr);
  179.     
  180.     pullDownMenu->AppendTextItem(ev, resFile, kMenuStrings, kChooseMovieStr, cChooseMovie);
  181.     pullDownMenu->AppendSeparator(ev);
  182.     pullDownMenu->AppendToggleItem(ev, resFile, kMenuStrings, kStopMovieStr, kStartMovieStr, cStartStopMovie);
  183.     pullDownMenu->AppendSeparator(ev);
  184.     pullDownMenu->AppendTextItem(ev, resFile, kMenuStrings, kLoopingStr, cSetMovieLooping);
  185.     pullDownMenu->AppendToggleItem(ev, resFile, kMenuStrings, kHideControllerStr, kShowControllerStr, cShowMovieController);
  186.     this->GetMenuBar(ev)->AdoptMenuLast(ev, pullDownMenu);
  187.  
  188.     RegisterIdle(ev, kMovieIdleFreq);
  189. }
  190.  
  191. //----------------------------------------------------------------------------------------
  192. //     CMoviePart::~CMoviePart
  193. //----------------------------------------------------------------------------------------
  194.  
  195. CMoviePart::~CMoviePart()
  196. {
  197.     // don't need to delete fDefaultPicture; it will go away automagically
  198.  
  199.     if (fInternalizedMovie)
  200.         delete fInternalizedMovie;
  201.         
  202.     CMovie::TerminateQuickTime();
  203. }
  204.  
  205. //----------------------------------------------------------------------------------------
  206. //    CMoviePart::InternalizeContent
  207. //----------------------------------------------------------------------------------------
  208.  
  209. void CMoviePart::InternalizeContent(Environment* ev, ODStorageUnit* storageUnit, FW_CCloneInfo* cloneInfo)
  210. {
  211.     if (storageUnit->Exists(ev, kODPropContents, this->GetPartKind(ev), 0))
  212.     {
  213.         InternalizeODFMovie(ev, storageUnit);
  214.     }
  215. #ifdef FW_BUILD_MAC
  216.     else if (storageUnit->Exists(ev, kODPropContents, FW_CPart::gMacMooVFileType, 0))
  217.     {
  218.         InternalizeMovieFile(ev, storageUnit);
  219.     }
  220. #endif
  221.     else 
  222.         FW_DEBUG_MESSAGE("CBitmapPart::InternalizeContent - Unkown type");
  223. }
  224.  
  225. //------------------------------------------------------------------------------
  226. // CMoviePart::InternalizeODFMovie
  227. //------------------------------------------------------------------------------
  228.  
  229. void CMoviePart::InternalizeODFMovie(Environment* ev, ODStorageUnit* storageUnit)
  230. {
  231.     FW_CStorageUnitSink sink(storageUnit, kODPropContents, this->GetPartKind(ev));
  232.     FW_CReadableStream archive(&sink);
  233.     
  234.     unsigned long movieSize = sink.GetReadableBytes();
  235.     if (movieSize)
  236.     {
  237.         FW_PlatformHandle movieHandle = FW_CMemoryManager::AllocateSystemHandle(movieSize);    // Throw if null
  238.         FW_ASSERT(movieHandle != NULL);
  239.         
  240.         FW_CMemoryManager::LockSystemHandle(movieHandle);
  241.         archive.Read(*movieHandle, movieSize);
  242.         FW_CMemoryManager::UnlockSystemHandle(movieHandle);
  243.         
  244.         if (fInternalizedMovie)
  245.             delete fInternalizedMovie;
  246.             
  247.         fInternalizedMovie = CMovie::CreateNewMovieFromHandle(movieHandle);
  248.     }
  249.     
  250.     if (storageUnit->Exists(ev, kContentAnnotationProp, (ODValueType)kContentAnnotationValue, 0))
  251.     {
  252.         FW_CStorageUnitSink sink2(storageUnit, kContentAnnotationProp, kContentAnnotationValue);
  253.         FW_CReadableStream archive2(&sink2);
  254.         archive2 >> fLooping;
  255.         archive2 >> fShowController;
  256.     }
  257. }
  258.  
  259. //------------------------------------------------------------------------------
  260. // CMoviePart::InternalizeMovieFile
  261. //------------------------------------------------------------------------------
  262.  
  263. void CMoviePart::InternalizeMovieFile(Environment* ev, ODStorageUnit* storageUnit)
  264. {
  265.     if (storageUnit->Exists(ev, kODPropContents, FW_CPart::gMachfsDataType, 0))
  266.     {
  267.         storageUnit->Focus(ev, kODPropContents, kODPosUndefined, FW_CPart::gMachfsDataType, 0, kODPosUndefined);
  268.  
  269.         // Get the HFS flavor
  270.         HFSFlavor hfsInfo;
  271.         
  272.         FW_CByteArray byteArray;
  273.         storageUnit->GetValue(ev, sizeof(HFSFlavor), byteArray);
  274.         byteArray.CopyBuffer(&hfsInfo, sizeof(HFSFlavor));
  275.         
  276.         if (fInternalizedMovie)
  277.             delete fInternalizedMovie;
  278.  
  279.         // Use the FileSpec in HFS flavor to open the file's data fork
  280.         FW_CFileSpecification fileSpec(hfsInfo.fileSpec);
  281.  
  282.         fInternalizedMovie = CMovie::CreateNewMovieFromFile(fileSpec);
  283.     }
  284. }
  285.  
  286. //----------------------------------------------------------------------------------------
  287. // CMoviePart::ExternalizeContent
  288. //----------------------------------------------------------------------------------------
  289.  
  290. void CMoviePart::ExternalizeContent(Environment* ev, ODStorageUnit* storageUnit, FW_CCloneInfo* cloneInfo)
  291. {
  292.     FW_UNUSED(cloneInfo);
  293.     
  294.     FW_PlatformHandle movieHandle = NULL;
  295.     
  296.     CMovieFrame* theMovieFrame;
  297.     if ((theMovieFrame = (CMovieFrame*)this->GetActiveFrame(ev)) != NULL)
  298.     {
  299.         CMovie* theMovie;
  300.         if ((theMovie = theMovieFrame->GetMovie(ev)) != NULL)
  301.             movieHandle = theMovie->GetAsHandle();
  302.     }
  303.     
  304.     ODValueType valueType = this->GetPartKind(ev);
  305.  
  306.     // ----- Clear the value -----
  307.     storageUnit->Focus(ev, kODPropContents, kODPosUndefined, valueType, 0, kODPosUndefined);
  308.     storageUnit->Remove(ev);
  309.     storageUnit->AddValue(ev, valueType);
  310.  
  311.     if (movieHandle)
  312.     {
  313.         FW_CStorageUnitSink sink(storageUnit, kODPropContents, valueType);
  314.         FW_CWritableStream archive(&sink);
  315.  
  316.         FW_CMemoryManager::LockSystemHandle(movieHandle);
  317.         archive.Write(*movieHandle, FW_CMemoryManager::GetSystemHandleSize(movieHandle));
  318.         FW_CMemoryManager::UnlockSystemHandle(movieHandle);
  319.         FW_CMemoryManager::FreeSystemHandle(movieHandle);
  320.         
  321.     }
  322.     
  323.     {
  324.         //    FW_SUAddPropValue correctly test if the property and value exist before
  325.         //    trying to create them
  326.         FW_SUAddPropValue(ev, storageUnit, kContentAnnotationProp, kContentAnnotationValue);
  327.         
  328.         FW_CStorageUnitSink sink2(storageUnit, kContentAnnotationProp, kContentAnnotationValue);
  329.         FW_CWritableStream archive(&sink2);
  330.         archive << fLooping;
  331.         archive << fShowController;
  332.     }
  333. }
  334.  
  335. //----------------------------------------------------------------------------------------
  336. //     CMoviePart::NewFrame
  337. //----------------------------------------------------------------------------------------
  338.  
  339. FW_CFrame* CMoviePart::NewFrame(Environment* ev, 
  340.                                 ODFrame* odFrame, 
  341.                                 FW_CPresentation* presentation, 
  342.                                 FW_Boolean fromStorage)
  343. {
  344.     FW_UNUSED(fromStorage);
  345.     
  346.     CMovie* theMovie = NULL;
  347.     
  348.     if (this->GetActiveFrame(ev))
  349.         theMovie = ((CMovieFrame*)this->GetActiveFrame(ev))->GetMovie(ev);
  350.         
  351.     return new CMovieFrame(ev, odFrame, presentation, this, theMovie);
  352. }
  353.  
  354. //----------------------------------------------------------------------------------------
  355. // CMoviePart::DoIdle
  356. //----------------------------------------------------------------------------------------
  357.  
  358. FW_Boolean CMoviePart::DoIdle(Environment* ev, const FW_CNullEvent& theNullEvent)
  359. {
  360.     FW_UNUSED(theNullEvent);
  361.  
  362.     FW_CPresentationFrameIterator iter(fPresentation);
  363.     for (CMovieFrame* movieFrame = (CMovieFrame*)iter.First(); iter.IsNotComplete(); movieFrame = (CMovieFrame*)iter.Next())
  364.         movieFrame->UpdateMovie(ev);
  365.  
  366.     return TRUE;
  367. }
  368.  
  369. //------------------------------------------------------------------------------
  370. // CMoviePart::DoMenu
  371. //------------------------------------------------------------------------------
  372.  
  373. FW_Boolean CMoviePart::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
  374. {
  375.     FW_Boolean menuHandled = TRUE;
  376.     
  377.     switch (theMenuEvent.GetCommandID(ev))
  378.     {
  379.         case cChooseMovie:
  380.                 this->ChooseMovie(ev);
  381.                 break;
  382.                 
  383.         case cSetMovieLooping:
  384.                 this->ToggleLooping(ev);
  385.                 break;
  386.                 
  387.         case cShowMovieController:
  388.                 this->ToggleController(ev);
  389.                 break;
  390.                 
  391.         default:
  392.             menuHandled = FALSE;
  393.     }            
  394.     
  395.     return menuHandled;
  396. }
  397.  
  398. //----------------------------------------------------------------------------------------
  399. // CMoviePart::GetMovieFromUser
  400. //----------------------------------------------------------------------------------------
  401.  
  402. FW_Boolean CMoviePart::GetMovieFileFromUser(Environment* ev, FW_CFileSpecification& fileSpec) const
  403. {
  404.     SFTypeList typeList;
  405.     StandardFileReply reply;
  406.  
  407.     typeList[0] = MovieFileType;
  408.     
  409.     this->GetSession(ev)->GetWindowState(ev)->DeactivateFrontWindows(ev);
  410.     ::StandardGetFilePreview ((FileFilterUPP)NULL, 1, typeList, &reply);
  411.     this->GetSession(ev)->GetWindowState(ev)->ActivateFrontWindows(ev);
  412.  
  413.     if (reply.sfGood == true)
  414.         fileSpec = FW_CFileSpecification(reply.sfFile);
  415.     
  416.     return reply.sfGood;
  417. }
  418.  
  419. //----------------------------------------------------------------------------------------
  420. // CMoviePart::ChooseMovie
  421. //----------------------------------------------------------------------------------------
  422.  
  423. void CMoviePart::ChooseMovie(Environment* ev)
  424. {
  425.     FW_CFileSpecification fileSpec(FW_CString32("Untitled"));
  426.     
  427.     if (this->GetMovieFileFromUser(ev, fileSpec))
  428.     {
  429.         // ----- I don't need the default picture anymore ----- 
  430.         if ((const void*)fDefaultPicture != NULL)
  431.         {
  432.             FW_PPicture nullPict;
  433.             fDefaultPicture = nullPict;
  434.         }    
  435.     
  436.         FW_CPresentationFrameIterator iter(fPresentation);
  437.         for (CMovieFrame* frame = (CMovieFrame*)iter.First(); iter.IsNotComplete(); frame = (CMovieFrame*)iter.Next())
  438.             frame->AdoptNewMovie(ev, CMovie::CreateNewMovieFromFile(fileSpec));
  439.     }
  440. }
  441.  
  442. //----------------------------------------------------------------------------------------
  443. // CMoviePart::ToggleLooping
  444. //----------------------------------------------------------------------------------------
  445.  
  446. void CMoviePart::ToggleLooping(Environment* ev)
  447. {
  448.     fLooping = !fLooping;
  449.     
  450.     FW_CPresentationFrameIterator iter(fPresentation);
  451.     for (CMovieFrame* frame = (CMovieFrame*)iter.First(); iter.IsNotComplete(); frame = (CMovieFrame*)iter.Next())
  452.         frame->ToggleLooping(ev, fLooping);
  453. }
  454.  
  455. //----------------------------------------------------------------------------------------
  456. // CMoviePart::ToggleController
  457. //----------------------------------------------------------------------------------------
  458.  
  459. void CMoviePart::ToggleController(Environment* ev)
  460. {
  461.     fShowController = !fShowController;
  462.     
  463.     FW_CPresentationFrameIterator iter(fPresentation);
  464.     for (CMovieFrame* frame = (CMovieFrame*)iter.First(); iter.IsNotComplete(); frame = (CMovieFrame*)iter.Next())
  465.         frame->ToggleController(ev, fShowController);
  466. }
  467.  
  468. //----------------------------------------------------------------------------------------
  469. // CMoviePart::GetDefaultPicture
  470. //----------------------------------------------------------------------------------------
  471.  
  472. FW_PPicture CMoviePart::GetDefaultPicture(Environment *ev) const
  473. {
  474.     // ----- Load the default picture only if I need it -----
  475.     if ((const void*)fDefaultPicture == NULL)
  476.     {
  477.         FW_CSharedLibraryResourceFile resFile;    // Open the resource file of this shared library
  478.         ((CMoviePart*)this)->fDefaultPicture = FW_PPicture(resFile, kDefaultMoviePicture);
  479.     }
  480.     
  481.     return fDefaultPicture;
  482. }
  483.  
  484.  
  485.